﻿import mx.utils.Delegate;
import mx.events.EventDispatcher;
class cn.eminia.mn.XmlLogFileV2 {
	public var oconfig:Object;
	public var addEventListener:Function;
	private var dispatchEvent:Function;
	public var removeEventListener:Function;
	public static var isLoaded:Boolean = false;
	public function XmlLogFileV2() {
	}
	private function load(file:String) {
		var xml:XML = new XML();
		xml.ignoreWhite = true;
		var t = this;
		EventDispatcher.initialize(this);
		xml.onLoad = Delegate.create(this, function (success) {
			if (success) {
				isLoaded = true;
				oconfig = getChildAsObject(xml.childNodes[0]);
				//_A_.broadcastMessage("ok",oconfig);
				//类内部直接在这调用你想执行的函数就行了.
				//这个事件是发送给类外部的接收者的
				dispatchEvent({type:"my_event", target:oconfig, a:1});
			} else {
				throw new Error('Data link failure');
			}
		});
		xml.load(file);
	}
	public static function getChildAsObject(xObj) {
		var obj = new Object();
		var a:Object, c:Object, nName:String, nType:Number, nValue:String, cCount:Number;
		for (a in xObj.attributes) {
			obj[a] = xObj.attributes[a];
		}
		for (c in xObj.childNodes) {
			nName = xObj.childNodes[c].nodeName;
			nType = xObj.childNodes[c].nodeType;
			nValue = xObj.childNodes[c].nodeValue;
			if (nType == 3) {
				obj._value = nValue;
				obj._type = 'text';
			}
			if (nType == 1 && nName != null) {
				if (obj[nName] == null) {
					obj[nName] = XmlLogFileV2.getChildAsObject(xObj.childNodes[c]);
				} else if (obj[nName]._type != 'array') {
					obj[nName] = [obj[nName]];
					obj[nName]._type = 'array';
				}
				if (obj[nName]._type == 'array') {
					obj[nName].unshift(XmlLogFileV2.getChildAsObject(xObj.childNodes[c]));
				}
			}
		}
		return obj;
	}
}
